<?php
class Tourism_order_model extends CI_Model
{
    private $table = 'tourism_order';
    private $table_2 = 'tourism_order_product';
    private $table_3 = 'user';

    public function insertTourismOrder($postData)
    {
        $data = array(
            'order_sn'          => getOrderSn(),
            'order_main_sn'     => $postData['order_main_sn'],
            'seller_uid'        => $postData['seller_uid'],
            'address_id'        => $postData['address_id'],
            'payer_uid'         => $postData['payer_uid'],
            'user_name'         => $postData['user_name'],
            'order_state'       => 1,
            'order_status'      => 2,
            'actual_price'      => 0,//$postData['actual_price'],
            'vanguard'          => $postData['vanguard'],
            'message'           => $postData['message'],
            'logistics_company' => '',
            'logistics'         => '',
            'is_form'           => 2, //1电脑端2手机端3 其他
            'detail'            => $postData['detail'],
            'order_price'       => 0,//$postData['order_price'],
            'transport_cost'    => $postData['transport_cost'],
            'payment_type'      => $postData['payment_type'],//$postData['payment_type'],
            'pay_bank'          => 0,
            'order_pay_total'   => 0,
            'pay_time'          => '0000-00-00 00:00:00',
            'send_time'         => '0000-00-00 00:00:00',
            'receive_time'      => '0000-00-00 00:00:00',
            'receive_time'      => '0000-00-00 00:00:00',
            'updated_at'        => '0000-00-00 00:00:00',
            'created_at'        => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function updateBatch($postData)
    {
        if (empty($postData)) {
            return false;
        }
        $this->db->update_batch($this->table, $postData, 'order_id');
        return $this->db->affected_rows();
    }

    public function updateStatus($order_sn, $params)
    {
        $this->db->where('order_sn',$order_sn);
        return $this->db->update($this->table,$params);
    }

    public function updateByOrderMainSn($orderMainSn, $params)
    {
        $this->db->where('order_main_sn',$orderMainSn);
        return $this->db->update($this->table,$params);
    }

    public function updateByOrderId($orderId, $params)
    {
        $this->db->where('order_id',$orderId);
        return $this->db->update($this->table, $params);
    }

    //获取商品goods_attr_id,number
    public function findTourismGoods($order_sn, $userToPay = false)
    {
        $this->db->select('tourism_order_product.number,tourism_order_product.pay_amount,tourism_order_product.goods_attr_id');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_id = tourism_order.order_id', 'inner');

        if ($userToPay) {
            $this->db->where('tourism_order.order_sn', $order_sn);
        } else {
            $this->db->where('tourism_order.order_main_sn', $order_sn);
        }
        return $this->db->get();
    }

    public function total($uid, $status = 0, $keyword)
    {
        $this->db->select('tourism_order_product.order_id');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_id = tourism_order.order_id', 'inner');
        $this->db->join($this->table_3 . ' AS user', 'user.uid = tourism_order.seller_uid', 'inner');
        $this->db->where('payer_uid', $uid);
        $order_state = 0;
        $order_status = 0;
        switch($status) {
            case 1://未付款
                $order_state = 1;
                $order_status = 2;
                break;
            case 2://已付款 待发货
                $order_state = 2;
                $order_status = 3;
                break;
            case 3://带收货
                $order_state = 2;
                $order_status = 4;
                break;
            case 4://带评价
                $order_state = 3;
                $order_status =5;
                break;
            default:
                break;
        }
        if($keyword){
            $this->db->like('goods_name', $keyword);
        }
        if ($order_state && $order_status) {
            $this->db->where('order_state', $order_state);
            $this->db->where('order_status', $order_status);
        }
        return $this->db->count_all_results();
    }

    //
    public function findByUid($uid, $status = 0, $count, $start = 0, $keyword)
    {
        $this->db->select('tourism_order.order_sn,tourism_order.order_id,tourism_order.order_state,tourism_order.order_status,tourism_order.order_pay_total,tourism_order.transport_cost,
                        tourism_order_product.number,attribute_value,tourism_order_product.pay_amount,tourism_order_product.order_product_id,tourism_order_product.goods_image,
                        tourism_order_product.goods_name,tourism_order_product.shop_price,user.alias_name');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_2 . ' AS tourism_order_product', 'tourism_order_product.order_id = tourism_order.order_id', 'inner');
        $this->db->join($this->table_3 . ' AS user', 'user.uid = tourism_order.seller_uid', 'inner');
        $this->db->where('payer_uid', $uid);
        $order_state = 0;
        $order_status = 0;
        switch($status) {
            case 1://未付款
                $order_state = 1;
                $order_status = 2;
                break;
            case 2://已付款 待发货
                $order_state = 2;
                $order_status = 3;
                break;
            case 3://带收货
                $order_state = 2;
                $order_status = 4;
                break;
            case 4://带评价
                $order_state = 3;
                $order_status =5;
                break;
            default:
                break;
        }
        if($keyword){
            $this->db->like('goods_name', $keyword);
        }
        if ($order_state && $order_status) {
            $this->db->where('order_state', $order_state);
            $this->db->where('order_status', $order_status);
        }
        $this->db->order_by('tourism_order_product.order_product_id', 'DESC');
        $this->db->limit($count,$start);
        return $this->db->get();
    }

    public function findById($id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('order_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findByOrderInfo($orderId)
    {
        $this->db->select('order_state,order_status,tourism_order.created_at,pay_time,send_time,receive_time,order_pay_total,transport_cost,
                        message,logistics_company,logistics,user.alias_name,
                        number,pay_amount,order_product_id,goods_image,goods_name');
        $this->db->from($this->table.' AS tourism_order');
        $this->db->join($this->table_2.' AS tourism_order_product', 'tourism_order.order_id = tourism_order_product.order_id', 'inner');
        $this->db->join($this->table_3.' AS user', 'tourism_order.seller_uid = user.uid', 'inner');
        $this->db->where('tourism_order.order_id', $orderId);

        return $this->db->get();
    }

    public function findAliasName($id)
    {
        $this->db->select('order_id,detail,order_sn,order_state,order_status,tourism_order.created_at,pay_time,send_time,receive_time,order_pay_total,pay_swim,transport_cost,
                            address_id,message,logistics_company,logistics,user.alias_name');
        $this->db->from($this->table.' AS tourism_order');
        $this->db->join($this->table_3.' AS user', 'tourism_order.seller_uid = user.uid', 'inner');
        $this->db->where('order_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findByOrderSn($orderSn)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('order_sn', $orderSn);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findByOrderMainSn($order_main_sn)
    {
        $this->db->select('tourism_order.order_id,tourism_order.order_pay_total,tourism_order.pay_swim,tourism_order.payer_uid,tourism_order.transport_cost,user.*');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_3 . ' AS user', 'tourism_order.seller_uid = user.uid');
        $this->db->where('tourism_order.order_main_sn', $order_main_sn);
        return $this->db->get();
    }

    public function findByOrderId($order_id)
    {
        $this->db->select('tourism_order.order_id,tourism_order.order_pay_total,tourism_order.pay_swim,tourism_order.payer_uid,tourism_order.transport_cost,user.*');
        $this->db->from($this->table . ' AS tourism_order');
        $this->db->join($this->table_3 . ' AS user', 'tourism_order.seller_uid = user.uid');
        $this->db->where('tourism_order.order_id', $order_id);
        $result = $this->db->get();
        return $result->row() ? $result->row() : false;
    }

    /**
     * 统计一天一个用户购买了几次
     * @param $uid
     * @param $limit_day
     * @return mixed
     */
    public function findBayNum($uid, $goodsAttrId, $limit_day)
    {
        $start_day = date('Y-m-d 00:00:00',strtotime('-'.($limit_day-1).'day'));  //起始时间
        $end_day = date('Y-m-d 23:59:59'); // 结束时间
        $this->db->select('SUM(number) AS `total`');
        $this->db->from($this->table.' AS tourism_order');
        $this->db->join($this->table_2. ' AS tourism_order_product', 'tourism_order_product.order_id = tourism_order.order_id', 'INNER');
        $this->db->where('payer_uid', $uid);
        $this->db->where('goods_attr_id', $goodsAttrId);
        $this->db->where('tourism_order.created_at >=', $start_day);
        $this->db->where('tourism_order.created_at <=', $end_day);
        $this->db->where('order_state >=', 1);
        $this->db->where('order_status >', 1);
        $result = $this->db->get();
        return $result->row()->total;
    }

    /**
     * 获取主订单支付下，改运费，刷新页面要支付多价格 和 抵扣多少U币；
     * @param $order_main_sn
     * @return mixed
     */
    public function getPayResultCount($order_main_sn)
    {
        $this->db->select('SUM(actual_price) AS `total_actual_price` , SUM(pay_swim) AS `total_pay_swim` , SUM(transport_cost) AS `total_transport_cost`');
        $this->db->from($this->table);
        $this->db->where('order_main_sn', $order_main_sn);
        return $this->db->get();
    }

    public function orderStatusTotal($params = array(), $state = false)
    {
        $this->db->select('tourism_order.order_status, count(tourism_order.order_status) AS status_count');
        $this->db->from($this->table . ' AS tourism_order');
        if (!empty($params['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $params['seller_uid']);
        }
        if (!empty($params['payer_uid'])) {
            $this->db->where('tourism_order.payer_uid', $params['payer_uid']);
        }
        if (!empty($params['start_date'])) {
            $this->db->where('tourism_order.created_at >', $params['start_date'] . ' 00:00:00');
        }
        if (!empty($params['end_date'])) {
            $this->db->where('tourism_order.created_at <', $params['end_date'] . ' 23:59:59');
        }
        $this->db->group_by('tourism_order.order_status');
        $result = $this->db->get();
        if ($state) {
            $rows = array();
            if ($result->num_rows() > 0) {
                foreach ($result->result_array() as $row) {
                    $rows[$row['order_status']] = $row['status_count'];
                }
            }
            return $rows;
        }
        return $result;
    }
}